package com.ys.erp.stage_pet_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ys.erp.entity.service.ICatService;
import com.ys.erp.entity.data.bean.Cat;
import com.ys.erp.framework.bean.dto.StageEmpDTO;
import com.ys.erp.framework.response.PageDTO;
import com.ys.erp.stage_pet_service.bean.condition.ProcurementCondition;
import com.ys.erp.stage_pet_service.bean.vo.ProcurementVo;
import io.netty.util.internal.ObjectUtil;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
@RefreshScope
@Log4j
public class PurchaseService {
    private final ICatService iCatService;

    public PageDTO<ProcurementVo> ProcurementTransportation(StageEmpDTO empDto, @Valid ProcurementCondition.ProcurementCon con) {
        // 构造查询条件
        QueryWrapper<Cat> catQw = new QueryWrapper<>();

        // 价格档位 最大
        if(ObjectUtils.isNotEmpty(con.getMaxMoney())) {
            catQw.lambda().le(Cat::getFixedPrice,(con.getMaxMoney()));
        }

        // 价格档位 最小
        if (ObjectUtils.isNotEmpty(con.getMinMoney())) {
            catQw.lambda().ge(Cat::getFixedPrice,(con.getMinMoney()));
        }

        // 宠物品种
        if(ObjectUtils.isNotEmpty(con.getBreedId())) {
            catQw.lambda().eq(Cat::getBreedId, Long.valueOf(con.getBreedId()));
        }

        // 宠物毛色
        if(ObjectUtils.isNotEmpty(con.getGrainColorCode())) {
            catQw.lambda().like(Cat::getGrainColorCode, con.getGrainColorCode());
        }

        // 宠物编号
        if(ObjectUtils.isNotEmpty(con.getCatCode())) {
            catQw.lambda().eq(Cat::getCatCode, con.getCatCode());
        }

        // 脚特征
        if(ObjectUtils.isNotEmpty(con.getFootFeatures())) {
            catQw.lambda().eq(Cat::getTraitFoot, con.getFootFeatures());
        }

        // 笼位

        // 所在仓店
        if(ObjectUtils.isNotEmpty(con.getShopCode())) {
            catQw.lambda().eq(Cat::getShopCode, con.getShopCode());
        }

        // 宠物类型
        if(ObjectUtils.isNotEmpty(con.getPetType())) {
            catQw.lambda().eq(Cat::getPetType, Integer.valueOf(con.getPetType()));
        }

        // 疫苗情况
        if(ObjectUtils.isNotEmpty(con.getVaccineNumList())) {
             catQw.lambda().in(Cat::getVaccinPrice, con.getVaccineNumList());
        }

       // 是否特价
        if(ObjectUtils.isNotEmpty(con.getIsSpecialOffer())) {
            catQw.lambda().eq(Cat::getIsShow, con.getIsSpecialOffer().intValue());
        }

        // 创建分页条件
        Page<Cat> catPage = new Page<>(con.getCurrent(), con.getSize());
        Page<Cat> page = iCatService.page(catPage, catQw);

        // 判断page.record是否为空，如果为空直接返回
        if(page.getRecords().isEmpty()) {
            return new PageDTO<>(1L, 0L, new ArrayList<>());
        }

        ArrayList<ProcurementVo> results = new ArrayList<>();
        for(Cat cat : page.getRecords()) {
            ProcurementVo vo = new ProcurementVo();
            vo.setCatCode(cat.getCatCode());
            vo.setBreed(cat.getBreed());
            vo.setTraitFoot(cat.getTraitFoot());
            vo.setGrainColor(cat.getGrainColor());
            vo.setSex((cat.getSex()));
            vo.setBirthTime(String.valueOf(cat.getBirthTime()));
            vo.setShopName(cat.getShopName());
            vo.setVaccinNumber(cat.getVaccinPrice());
            vo.setCompensate(String.valueOf(cat.getFixedPrice()));
            vo.setPhotos(Collections.singletonList(cat.getPhotos()));
            vo.setIsShow(cat.getIsShow());
            vo.setIsTop(cat.getIsTop());
            vo.setIsAdopt(cat.getIsAdopt());
            vo.setIsSterilisation(cat.getIsSterilisation());
            vo.setTenantId(String.valueOf(cat.getTenantId()));
            vo.setTenantTwoId(String.valueOf(cat.getTenantTwoId()));
            vo.setCatStatus(cat.getCatStatus());

            results.add(vo);
        }
        return new PageDTO<>(page.getPages(), page.getTotal(), results);
    }

}
